<?php
require 'common.inc.php';
//遍历获取所有邮箱管理模块下的信息
$result = $db->query("SELECT * FROM {$DT_PRE}email_user where pushstatus=3 or pushstatus=4  ORDER BY itemid");
$curr_time = time();
sleep(rand(0,10));
if($db->affected_rows($result)) {
    while($user = $db->fetch_array($result)) {
		$pushlist = pushlist($user,$curr_time);         //获取推送的查询列表
		$itemid = $user['itemid'];
		$email = $user['email'];
		$cur_time = time();
		$u_pushtime = $db->get_one("SELECT pushtime FROM {$DT_PRE}email_user WHERE itemid ='$itemid'");
        echo  '[email]'. $user['email'].'[itemid]'.$itemid.'=><br>';
        $r  =  $db->get_one("SELECT * FROM {$DT_PRE}pushemaillog where itemid = '{$itemid}'  ORDER BY createtime desc limit 1 ");
		$db->query("INSERT INTO {$db->pre}pushemaillog (itemid,createtime) VALUES ('{$itemid}','{$cur_time}')");
		if($r){
			if(time() - $r['createtime'] < 60 ){
				continue;
			}
		}
        if(!$pushlist || empty($user['filtercategory']) ){
			echo  '=> nodata to push ......<br>';
		    continue;
		} 
		//10分钟内不再重新发送，解决同时间发送多条重复信息
		if( time() - $u_pushtime['pushtime'] < 600 ){
			echo 'current time no need push msg......<br>';
		    continue;
		} 

    	if($user['pushemail']){
            $md5 = md5(uniqid());
    		$DT['push_filename']        = 'html_push/push-'.$md5;
            $DT['push_email_filename']  = 'html_push/push-'.$md5.'-email';
        	//生成html
        	tohtml('push');
        	$filename = $CFG['com_dir'] ? DT_ROOT.'/'.$DT['push_filename'].'.'.$DT['file_ext'] : DT_CACHE.'/index.inc.html';
        	echo  'filename=>'.$filename.'</br>' ;
        }
        

       
        /*****************************
         * 发送邮件
         * *************************/
         if($user['pushemail']){
           $emails =  explode(';',$user['email']);
            foreach ($emails as $email){
                echo '<br>pushemail===>'.$email.'</br>';
				//$title = '您有新的招标信息-合信招标订阅';
				$title=$pushlist[0]['title'];
				$content = file_get_contents($DT['push_email_filename'].'.html');
				$sender = 'admin@china-hxzb.com';
				$rs_send_mail = send_mail($email, $title, $content, $sender);
            }
        
         }
        $_pushtime = time();
        $db->query("update {$DT_PRE}email_user  set pushtime = '$_pushtime' where itemid ='$itemid'");

    }
}



?>
